//
//  QYSearchDetailTableViewCell.m
//  QingYue
//
//  Created by Lei_Zhong_Xun on 2017/6/28.
//  Copyright © 2017年 com.lanou.product_A. All rights reserved.
//

#import "QYSearchDetailTableViewCell.h"
#import "QYBookDetailViewController.h"

@implementation QYSearchDetailTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.titleImgView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.contentLabel];
        [self addSubview:self.detailButton];
    }
    return self;
}

- (void)setDetailModel:(QYSearchDetailModel *)detailModel
{
    _detailModel = detailModel;
    [_titleImgView sd_setImageWithURL:[NSURL URLWithString:_detailModel.ImgUrl] placeholderImage:[UIImage imageNamed:@"图书占位2"]];
    _nameLabel.text = _detailModel.BookName;
    _contentLabel.text = [NSString stringWithFormat:@"作者: %@\n出版社: %@", _detailModel.Author, _detailModel.Publisher];
    
}

#pragma mark - 详情按钮实现方法
- (void)detailButtonClick
{
    QYBookDetailViewController *detailVC = [[QYBookDetailViewController alloc] init];
    detailVC.isbn = _detailModel.isbn;
    [[self viewController].navigationController pushViewController:detailVC animated:YES];
}

//获取view的controller
- (UIViewController *)viewController
{
    for (UIView* next = [self superview]; next; next = next.superview) {
        
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [_titleImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(10);
        make.top.equalTo(self.mas_top).offset(10);
        make.height.equalTo(@100);
        make.width.equalTo(@100);
    }];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_titleImgView.mas_right).offset(20);
        make.top.equalTo(self.mas_top).offset(20);
        make.right.equalTo(self.mas_right).offset(- 10);
        make.height.equalTo(@15);
    }];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@26);
        make.top.equalTo(_nameLabel.mas_bottom).offset(10);
        make.left.equalTo(_titleImgView.mas_right).offset(25);
        make.right.equalTo(self.mas_right).offset(- 10);
    }];
    [_detailButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(- 2);
        make.height.equalTo(@15);
        make.right.equalTo(self.mas_right).offset(- 25);
        make.width.equalTo(@25);
    }];
}


- (UIImageView *)titleImgView
{
    if (!_titleImgView) {
        _titleImgView = [[UIImageView alloc] init];
        _titleImgView.backgroundColor = [UIColor clearColor];
    }
    return _titleImgView;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"什么鬼";
        _nameLabel.backgroundColor = [UIColor clearColor];
        _nameLabel.font = Font(13);
    }
    return _nameLabel;
}
- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [[UILabel alloc] init];
        _contentLabel.backgroundColor = [UIColor clearColor];
        _contentLabel.numberOfLines = 0;
        _contentLabel.textColor = RGB(88, 89, 89, 1.0);
        _contentLabel.font = Font(10);
    }
    return _contentLabel;
}
- (UIButton *)detailButton
{
    if (!_detailButton) {
        _detailButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_detailButton setTitle:@"详情" forState:UIControlStateNormal];
        _detailButton.titleLabel.font = Font(10);
        [_detailButton setTitleColor:RGB(0, 119, 200, 1.0) forState:UIControlStateNormal];
        [_detailButton addTarget:self action:@selector(detailButtonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _detailButton;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
